<template>
    <div class="main-body">
        <div class="top-box">
            <div>
                <el-button type="primary" @click="saveDetail">保存填报</el-button>
                <el-button @click="countWnv">计算WNV</el-button>
            </div>
            <div class="main-title">
                {{wnvMonth}}数据填报
            </div>
            <div>
                <el-date-picker
                    v-model="wnvMonth"
                    type="month"
                    format="yyyy-MM"
                    value-format="yyyy-MM"
                    placeholder="选择月"
                    @change="getList"
                >
                </el-date-picker>
                <el-button type="primary" @click="getList">查询</el-button>
            </div>
        </div>
        <div class="table-box">
            <div v-for="(item,index) of wnvMainDetail" :key="index">
                <div class="title">{{item.levelNum}}、{{item.indexDesc}}</div>
                <div class="edit-item">
                    <div v-for="(child,idx) of item.children" :key="idx" class="edit-item-item">
                        <div class="label item">{{item.levelNum}}.{{idx+1}}、{{child.indexDesc}}</div>
                        <div class="value item" v-if="child.optionsList.length === 0">
                            <el-input v-model="child.indexOptions" type="number" v-if="child.biz1 === 'NUM'"></el-input>
                            <el-input v-model="child.indexOptions" type="text" v-else></el-input>
                        </div>
                        <div class="value item" v-else>
                            <el-select v-model="child.indexOptions" placeholder="请选择">
                                <el-option
                                    v-for="item in child.optionsList"
                                    :key="item.optionsCode"
                                    :label="item.optionsDesc"
                                    :value="item.optionsCode">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="title border-top-none">公式</div>
            <div class="edit-item">
<!--                <div class="edit-item-item">-->
<!--                    <div class="label item border-bottom-none">-->
<!--                        参数μ-->
<!--                        <img src="@/assets/img/2.png" class="item-img" style="margin-left: 10px" alt="" srcset="">-->
<!--                    </div>-->
<!--                    <div class="value item border-bottom-none">-->
<!--                        <el-input v-model="miu" type="number"></el-input>-->
<!--                    </div>-->
<!--                </div>-->
                <div class="edit-item-item">
                    <div class="label item border-bottom-none">WNV计算公式</div>
                    <div class="value item border-bottom-none">
                        <img src="@/assets/img/1.png" class="item-img" alt="" srcset="">
                    </div>
                </div>

            </div>
            <div class="title border-top-none">WNV计算结果：{{WNVResource}}</div>
            <div class="edit-item">
                <div class="edit-item-item" style="width: 100%">
                    <div class="label item border-bottom-none">
                        结果描述
                    </div>
                    <div class="value item border-bottom-none">
                        <el-input v-model="WNVResourceDec"></el-input>
                    </div>
                </div>

            </div>
        </div>
    </div>
</template>

<script>


export default {
	data () {
		return {
            wnvMonth: wfy.format('yyyy-MM', new Date()),
            wnvMainDetail: null,
            WNVResource: 0,
            wnvStatus: "",
            miu: "",
            wnvMainId: "",
            WNVResourceDec: ""
		}
	},
	components: {

	},
	created () {
	},
	activated () {

	},
	mounted () {
        this.getList();
	},
	methods: {
        countWnv(){
            if(this.wnvStatus === "A"){
                return this.$message.warning("已审核，不能再次计算！")
            }
            let cl = this.wnvMainDetail.find((item) => {
                return item.indexKindId === "C"
            })
            console.log("🚀 ~ cl", cl)
            let clList = cl.children;
            let f = clList.find((item) => {
                return item.indexDesc.includes("产卵数")
            })
            let a = clList.find((item) => {
                return item.indexDesc.includes("发育时间")
            })
            let e = clList.find((item) => {
                return item.indexDesc.includes("卵孵率")
            })
            let miu = clList.find((item) => {
                return item.indexDesc.includes("μ")
            })
            if(wfy.empty(f.indexOptions)){
                this.$message.error("产卵数不能为空")
            }
            if(wfy.empty(a.indexOptions)){
                this.$message.error("发育时间不能为空")
            }
            if(wfy.empty(e.indexOptions)){
                this.$message.error("卵孵率不能为空")
            }
            if(wfy.empty(miu.indexOptions)){
                this.$message.error("参数μ不能为空")
            }
            if(Number(miu.indexOptions) === 0){
                this.$message.error("参数μ不能为0")
            }
            this.WNVResource = wfy.setTwoNum(wfy.accDiv(wfy.accMul(Number(f.indexOptions),Number(e.indexOptions)),wfy.accMul(Number(a.indexOptions),Number(miu.indexOptions))))
        },
        // 设置指标数据
        setTargetData(data){
            this.wnvMainId = data.wnvMainId;
            let dtlList = [];
            let levelNum = 0;
            data.wnvDtlList.forEach((item) => {
                if(item.indexKindId === "1"){
                    levelNum++;
                    dtlList.push({
                        ...item,
                        levelNum: levelNum,
                        children: []
                    })
                }
                if(item.indexKindId === "2"){
                    let findResult = dtlList.find((zb) => {
                        return zb.indexId === item.preindexId
                    })
                    findResult.children.push(item)
                }
            })
            levelNum++;
            dtlList.push({
                indexDesc: "三级指标",
                levelNum: levelNum,
                indexKindId: "3",
                children: []
            })
            levelNum++;
            dtlList.push({
                indexDesc: "常量指标",
                levelNum: levelNum,
                indexKindId: "C",
                children: []
            })
            data.wnvDtlList.forEach((item) => {
                if(item.indexKindId === "3"){
                    let findResult = dtlList.find((zb) => {
                        return zb.indexKindId === "3"
                    })
                    findResult.children.push(item)
                }
                if(item.indexKindId === "C"){
                    let findResult = dtlList.find((zb) => {
                        return zb.indexKindId === "C"
                    })
                    findResult.children.push(item)
                }
            })
            return dtlList
        },
        saveDetail(){
            if(this.wnvStatus === "A"){
                return this.$message.warning("已审核，不能再次保存！")
            }
            console.log("🚀 ~ this.wnvMainDetail", this.wnvMainDetail)
            if(wfy.zeroEmpty(this.WNVResource)){
                return this.$message.warning("请先计算WNV结果");
            }
            let saveData = [];
            this.wnvMainDetail.forEach((item) => {
                item.children.forEach((child) => {
                    saveData.push({
                        indexOptions: child.indexOptions,
                        wnvDtlId: child.wnvDtlId
                    })
                })
            })
            this.$http({
                url: this.$http.adornUrl(`/admin/wnv/updWnvDtl`),
                method: 'post',
                data: this.$http.adornData({
                    wnvDtlList: saveData,
                    wnvStatus: "C",
                    wnvMonth: this.wnvMonth,
                    wnvMainId: this.wnvMainId,
                    wnvComTime: wfy.format('yyyy-MM-dd HH:mm:ss', new Date()),
                    wnvResult: this.WNVResource,
                    wnvResultDesc: this.WNVResourceDec
                })
            }).then(({ data }) => {
                console.log('🚀 ~ 保存', data)
                if(data.wnvResult){
                    this.$alert("填报成功","友情提醒");
                }
            })
        },
        getList(){
            var loading = this.$loading();
            this.$http({
                url: this.$http.adornUrl(`/admin/wnv/queryWnv`),
                method: 'post',
                data: this.$http.adornData({
                    wnvMonth: this.wnvMonth
                })
            }).then(({ data }) => {
                console.log('🚀 ~ 查询是否有', data)
                loading.close();
                if(wfy.empty(data.wnvMainId)){
                    this.$confirm('该月份还没有填报数据，是否初始化数据并填报?', '友情提醒', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.initMonth()
                    }).catch(() => { })
                }else{
                    this.wnvMainDetail = this.setTargetData(data);
                    console.log("🚀 ~ this.wnvMainDetail", this.wnvMainDetail)
                    this.WNVResource = data.wnvResult;
                    this.wnvStatus = data.wnvStatus;
                    this.WNVResourceDec = data.wnvResultDesc;
                }
            })
        },
        initMonth(){
            var loading = this.$loading();
            this.$http({
                url: this.$http.adornUrl(`/admin/wnv/initWnv`),
                method: 'post',
                data: this.$http.adornData({
                    wnvMonth: this.wnvMonth,
                    wnvMainId: wfy.uuid(32)
                })
            }).then(({ data }) => {
                console.log('🚀 ~ 初始化', data)
                loading.close();
                this.wnvMainDetail = this.setTargetData(data);
                console.log("🚀 ~ this.wnvMainDetail", this.wnvMainDetail)
                this.WNVResource = 0;
                this.WNVResourceDec = "";
            })
        }
	}
}
</script>

<style lang="scss" scoped>
.main-body{
    height: 100%;
    width: 100%;
}
.main-title{
    height: 40px;
    width: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: bold;
}
.top-box{
    display: flex;
    justify-content: space-between;
}
.table-box{
    height: calc(100% - 40px);
    width: 100%;
    margin-top: 20px;
    border: 1px solid #dddddd;
}
.title{
    background-color: #f2f2f2;
    height: 50px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #dddddd;
    //box-shadow: inset 0 0 5px rgb(30 159 255 / 40%);
    padding: 8px;
}
.edit-item{
    width: 100%;
    height: auto;
    min-height: 60px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    flex-wrap: wrap;
    .item{
        border-right: 1px solid #dddddd;
        border-bottom: 1px solid #dddddd;
    }
    .item:last-child{
        border-right: none;
    }
    .label{
        width: 200px;
        height: 100%;
        display: flex;
        align-items: center;
        //box-shadow: inset 0 0 5px rgb(30 159 255 / 40%);
        padding: 8px;
    }
    .value{
        width: calc(100% - 200px);
        height: 100%;
        //box-shadow: inset 0 0 5px rgb(30 159 255 / 40%);
        padding: 8px;
        box-sizing: border-box;
    }
    .el-select{
        width: 100%;
        height: 100%;
    }
    .edit-item-item{
        width: 33.3%;
        height: 60px;
        display: flex;
    }
}
.source{
    height: 80px;
    width: 100%;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    border-top: 1px solid #dddddd;
}
.border-top-none{
    border-top: none;
}
.border-bottom-none{
    border-bottom: none !important;
}
.item-img{
    height: 47px;
}
</style>
